﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Messaging;

namespace JSTT_SEV_PROJECT
{

    public class MessageData
    {
        private byte[] _messageBody;


        public byte[] MessageBody
        {
            get { return _messageBody; }
        }

        public MessageData(byte[] datas)
        {
            //_messageBody = (byte[]) datas.Clone();
            _messageBody = new byte[datas.Length];
            //Array.ConstrainedCopy();
            Buffer.BlockCopy(datas, 0, _messageBody, 0, datas.Length);
        }
    }


    public class MessageEventArgs : EventArgs
    {
        private readonly MessageData _messageData;

        public byte[] BinaryMessageBody
        {
            get { return _messageData.MessageBody; }
            
        }

        public MessageEventArgs(MessageData messageData)
        {
            _messageData = messageData;
        }
    }
    /// <summary>
    /// 定义委托，在新的消息来时进行广播
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void MessageHandler(Object sender, MessageEventArgs e);

    /// <summary>
    /// 处理MessageQueue类
    /// </summary>
    public class DTMessageQueue
    {
        private string MessageQueueName = ".\\private$\\JSTTProcessMessage";
        //private byte[] MessageDatas;
        private MessageQueue MessageQue = null;
        public event MessageHandler MessageReceived;
        public DTMessageQueue()
        {
            CreateMessageQueue(MessageQueueName);
           
            //MessageDatas = new byte[]{}; 
        }


        /// <summary>
        /// ReceiveMessage事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnMessageReceived(object sender, MessageEventArgs e)
        {
            if (MessageReceived != null)
            {
                MessageReceived(sender, e);
            }
        }

        /// <summary>
        /// 创建Message队列
        /// </summary>
        /// <param name="queuePath"></param>
        protected void CreateMessageQueue(string queuePath)
        {
            try
            {
                if (!MessageQueue.Exists(queuePath))
                {
                    MessageQueue.Create(queuePath);
                }
                MessageQue = new System.Messaging.MessageQueue(MessageQueueName);
            }
            catch (MessageQueueException e)
            {
                // 日志Log并退出
            }
        }

        /// <summary>
        /// 判断MessageQueue是否存在
        /// </summary>
        /// <param name="queuePath"></param>
        /// <returns></returns>
        protected bool IsMessageQueueExists(string queuePath)
        {
            if (!MessageQueue.Exists(queuePath))
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        public void SendMessage(Byte[] messageDatas)
        {
            if (messageDatas != null && MessageQue!=null )
            {
                if (!IsMessageQueueExists(MessageQueueName))
                {
                    CreateMessageQueue(MessageQueueName);
                }

                Message myMessage = new Message();
                myMessage.Body = messageDatas;
                myMessage.Formatter = new BinaryMessageFormatter();
                MessageQue.Send(myMessage);
            } 
        }

        public void DeleteMessageQueue()
        {
            if (!MessageQueue.Exists(MessageQueueName))
            {
                //MessageQue.Purge();
                MessageQueue.Delete(MessageQueueName);
            }
        }

        public void ReceiveMessage()
        {
            if (MessageQue != null)
            {
                MessageQue.Formatter = new BinaryMessageFormatter();
                System.Messaging.Message message = MessageQue.Receive();
                byte[] recvDatas = (byte[])message.Body;
                MessageEventArgs messageArgs = new MessageEventArgs(new MessageData(recvDatas));
                OnMessageReceived(this,messageArgs);
            }
        }
    }
}
